﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EntityComponentSystem.Framework;
using EntityComponentSystem.Demos.SpaceAttack.Components;
using EntityComponentSystem.Framework.Math;

namespace EntityComponentSystem.Demos.SpaceAttack.Systems
{
    public class InputMapper : ComponentMapper
    {
        public VelocityComponent Velocity { get; private set; }

        protected override void OnInit()
        {
            Velocity = Entity.GetComponent<VelocityComponent>();
        }
    }

    public interface IInputSystem : ISystemProcessor
    {

    }

    public class InputSystem : EntityComponentSystem.Framework.System<MovementMapper>, IInputSystem
    {
        public InputSystem(IGameEngine ge)
            : base(ge)
        {

        }

        protected override void OnUpdate(long timeDelta, float timeDeltaF)
        {
            if (Console.KeyAvailable)
            {
                Console.SetIn(System.IO.TextReader.Null);
                var vel = Vector2.Zero;

                do
                {
                    var key = Console.ReadKey();

                    if (key.Key == ConsoleKey.Escape)
                    {
                        Program.IsRunning = false;
                        return;
                    }

                    if (key.Key == ConsoleKey.D) vel.x += 1;
                    if (key.Key == ConsoleKey.A) vel.x -= 1;
                    if (key.Key == ConsoleKey.W) vel.y -= 1;
                    if (key.Key == ConsoleKey.S) vel.y += 1;
                } while (Console.KeyAvailable);

                foreach (var am in AllComponentMaps)
                    am.Velocity.SetVelocity(vel);
            }
        }

        protected override void OnDispose()
        {

        }

    }
}
